package com.zkthink.ceres.msgs.utils;

import com.alibaba.fastjson.JSON;
import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.*;
import com.tencent.xinge.bean.ios.Alert;
import com.tencent.xinge.bean.ios.Aps;
import com.tencent.xinge.push.app.PushAppRequest;
import com.zkthink.ceres.msgs.config.PushConfig;
import com.zkthink.ceres.sms.dto.PushMsgDTO;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;

/**
 * @Author Xiaoyan
 * @create 2021/11/19 15:41
 */
@Configuration
public class TencentPushUtil {

    @Autowired
    private static PushConfig pushConfig;


    //应用key
    public static String tencent_android_app_id;
    //应用密钥
    public static String tencent_android_secret_key;
    //应用密钥
    public static String tencent_ios_app_id;
    //应用密钥
    public static String tencent_ios_secret_key;
    //应用密钥
    public static String tencent_domain_url;    //应用密钥
    public static String tencent_environment;


    //推送目标 AccessID，可在 产品管理 页面获取。
    @Value("${tencentMobilePushConfig.androidAppId:}")
    private String tencentAndroidAppId;
    //推送密钥，可在【产品管理】 -【应用配置】页面获取。
    @Value("${tencentMobilePushConfig.androidSecretKey:}")
    private String tencentAndroidSecretKey;
    //推送目标 AccessID，可在 产品管理 页面获取。
    @Value("${tencentMobilePushConfig.iosAppId:}")
    private String tencentIosAppId;
    //推送密钥，可在【产品管理】 -【应用配置】页面获取。
    @Value("${tencentMobilePushConfig.iosSecretKey:}")
    private String tencentIosSecretKey;
    //请求接口服务域名地址，默认为请求信鸽平台的接口地址。使用时需要根据您产品的服务接入点选择 请求服务地址
    //广州服务接入点	https://api.tpns.tencent.com/
    //上海服务接入点	https://api.tpns.sh.tencent.com/
    //中国香港服务接入点	https://api.tpns.hk.tencent.com/
    //新加坡服务接入点	https://api.tpns.sgp.tencent.com/
    @Value("${tencentMobilePushConfig.domainUrl:}")
    private String tencentDomainUrl;
    //IOS是生产环境还是测试环境  dev：测试 product：生产
    @Value("${tencentMobilePushConfig.environment:}")
    private String tencentEnvironment;

    @PostConstruct
    public void init() {
        tencent_android_app_id = tencentAndroidAppId;
        tencent_android_secret_key = tencentAndroidSecretKey;
        tencent_ios_app_id = tencentIosAppId;
        tencent_ios_secret_key = tencentIosSecretKey;
        tencent_domain_url = tencentDomainUrl;
        tencent_environment = tencentEnvironment;
        System.out.println(
                "---初始化开放腾讯推送平台api参数---tencentAndroidAppId---" + tencent_android_app_id + "---tencentAndroidSecretKey---" + tencent_android_secret_key + "---tencentIosAppId---" + tencent_ios_app_id + "---tencentIosSecretKey---" + tencent_ios_secret_key + "---tencentDomainUrl---" + tencent_domain_url + "---tencentEnvironment---" + tencent_environment + "---");
    }


    /**
     * Android 单/多设备推送
     *
     * @param title     标题
     * @param content   内容
     * @param tokenList tokenList
     * @return
     */
    public static JSONObject pushTokenAndroid(String title, String content, ArrayList<String> tokenList) {
        XingeApp androidApp = new XingeApp.Builder()
                .appId(tencent_android_app_id)
                .secretKey(tencent_android_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.token);
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        pushAppRequest.setMessage(message);
        MessageAndroid messageAndroid = new MessageAndroid();
        message.setAndroid(messageAndroid);
        pushAppRequest.setToken_list(tokenList);
        return androidApp.pushApp(pushAppRequest);
    }


    /**
     * Android 单/多账号推送
     *
     * @param title       标题
     * @param content     内容
     * @param accountList accountList
     * @return
     */
    public static JSONObject pushAccountAndroid(String title, String content, ArrayList<String> accountList, PushMsgDTO dto) {
        XingeApp androidApp = new XingeApp.Builder()
                .appId(tencent_android_app_id)
                .secretKey(tencent_android_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.account);
        pushAppRequest.setPlatform(Platform.android);
        pushAppRequest.setMessage_type(MessageType.notify);
        pushAppRequest.setAccount_push_type(1);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageAndroid messageAndroid = new MessageAndroid();
        if (dto != null) {
            messageAndroid.setCustom_content(JSON.toJSON(dto).toString());
        }
        messageAndroid.setBadgeType(-2);
        message.setAndroid(messageAndroid);
        pushAppRequest.setMessage(message);
        pushAppRequest.setAccount_list(accountList);
        return androidApp.pushApp(pushAppRequest);
    }


    public static JSONObject pushAccountAndroidAndTime(String title, String content, ArrayList<String> accountList, String sendTime, PushMsgDTO dto) {
        XingeApp androidApp = new XingeApp.Builder()
                .appId(tencent_android_app_id)
                .secretKey(tencent_android_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.account);
        pushAppRequest.setPlatform(Platform.android);
        pushAppRequest.setMessage_type(MessageType.notify);
        pushAppRequest.setAccount_push_type(1);
        pushAppRequest.setSend_time(sendTime);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageAndroid messageAndroid = new MessageAndroid();
        if (dto != null) {
            messageAndroid.setCustom_content(JSON.toJSON(dto).toString());
        }
        messageAndroid.setBadgeType(-2);
        message.setAndroid(messageAndroid);
        pushAppRequest.setMessage(message);
        pushAppRequest.setAccount_list(accountList);
        return androidApp.pushApp(pushAppRequest);
    }

    /**
     * Android 全设备推送
     *
     * @param title   标题
     * @param content 内容
     * @return
     */
    public static JSONObject pushAllAndroid(String title, String content) {
        XingeApp androidApp = new XingeApp.Builder()
                .appId(tencent_android_app_id)
                .secretKey(tencent_android_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.all);
        pushAppRequest.setPlatform(Platform.android);
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageAndroid messageAndroid = new MessageAndroid();
        messageAndroid.setBadgeType(-2);
        message.setAndroid(messageAndroid);
        pushAppRequest.setMessage(message);
        return androidApp.pushApp(pushAppRequest);
    }

    /**
     * IOS 全设备推送
     *
     * @param title   标题
     * @param content 内容
     * @return
     */
    public static JSONObject pushAllIos(String title, String content) {
        XingeApp iosApp = new XingeApp.Builder()
                .appId(tencent_ios_app_id)
                .secretKey(tencent_ios_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.all);
        pushAppRequest.setEnvironment(Environment.valueOf(tencent_environment));
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        message.setIos(messageIOS);
        pushAppRequest.setMessage(message);
        return iosApp.pushApp(pushAppRequest);
    }

    /**
     * IOS 单/多账号推送
     *
     * @param title       标题
     * @param content     内容
     * @param accountList accountList
     * @return
     */
    public static JSONObject pushAccountIos(String title, String content, ArrayList<String> accountList) {
        XingeApp iosApp = new XingeApp.Builder()
                .appId(tencent_ios_app_id)
                .secretKey(tencent_ios_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.account);
        pushAppRequest.setEnvironment(Environment.valueOf(tencent_environment));
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        message.setIos(messageIOS);
        pushAppRequest.setMessage(message);
        pushAppRequest.setAccount_list(accountList);
        return iosApp.pushApp(pushAppRequest);
    }

    public static JSONObject pushAccountIos(String title, String content, ArrayList<String> accountList, PushMsgDTO dto) {
        XingeApp iosApp = new XingeApp.Builder()
                .appId(tencent_ios_app_id)
                .secretKey(tencent_ios_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.account);
        pushAppRequest.setEnvironment(Environment.valueOf(tencent_environment));
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        if (dto != null) {
            messageIOS.setCustom(JSON.toJSON(dto).toString());
        }
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        message.setIos(messageIOS);
        MessageAndroid messageAndroid = new MessageAndroid();

        pushAppRequest.setMessage(message);
        pushAppRequest.setAccount_list(accountList);
        return iosApp.pushApp(pushAppRequest);
    }

    public static JSONObject pushAccountIosAndTime(String title, String content, ArrayList<String> accountList, String sendTime) {
        XingeApp iosApp = new XingeApp.Builder()
                .appId(tencent_ios_app_id)
                .secretKey(tencent_ios_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.account);
        pushAppRequest.setEnvironment(Environment.valueOf(tencent_environment));
        pushAppRequest.setMessage_type(MessageType.notify);
        pushAppRequest.setSend_time(sendTime);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        message.setIos(messageIOS);
        pushAppRequest.setMessage(message);
        pushAppRequest.setAccount_list(accountList);
        return iosApp.pushApp(pushAppRequest);
    }

    /**
     * IOS 单/多设备推送
     *
     * @param title     标题
     * @param content   内容
     * @param tokenList tokenList
     * @return
     */
    public static JSONObject pushTokenIos(String title, String content, ArrayList<String> tokenList) {
        XingeApp iosApp = new XingeApp.Builder()
                .appId(tencent_ios_app_id)
                .secretKey(tencent_ios_secret_key)
                .domainUrl(tencent_domain_url)
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.token);
        pushAppRequest.setEnvironment(Environment.valueOf("dev"));
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        message.setIos(messageIOS);
        pushAppRequest.setMessage(message);
        pushAppRequest.setToken_list(tokenList);
        return iosApp.pushApp(pushAppRequest);
    }


    /**
     * appId: 1500024832
     * #推送密钥，可在【产品管理】 -【应用配置】页面获取。
     * secretKey: 8dabab46ca33f51fdff0156546caf2f0
     * #请求接口服务域名地址，默认为请求信鸽平台的接口地址。使用时需要根据您产品的服务接入点选择 请求服务地址
     * #广州服务接入点	https://api.tpns.tencent.com/
     * #上海服务接入点	https://api.tpns.sh.tencent.com/
     * #中国香港服务接入点	https://api.tpns.hk.tencent.com/
     * #新加坡服务接入点	https://api.tpns.sgp.tencent.com/
     * domainUrl: https://api.tpns.tencent.com/
     * #IOS是生产环境还是测试环境  dev：测试 product：生产
     * environment: dev
     */
    static XingeApp ceshiapp = new XingeApp.Builder()
            .appId("1500028788")
            .secretKey("8dabab46ca33f51fdff0156546caf2f0")
            .domainUrl("https://api.tpns.tencent.com/")
            .build();
    static XingeApp ceshiiosapp = new XingeApp.Builder()
            .appId("1600024833")
            .secretKey("594471ed178a6aa00d3d22fa8513ff0a")
            .domainUrl("https://api.tpns.tencent.com/")
            .build();

//    public static void main(String[] args) {
//        System.out.println(ceshiPushAllAndroid("测试1","测试1"));
////        System.out.println(ceshiPushAllIos("测试1","测试1"));
//    }

    /**
     * Android 全设备推送
     *
     * @param title   标题
     * @param content 内容
     * @return
     */
//    public static JSONObject ceshiPushAllAndroid(String title, String content) {
//        PushAppRequest pushAppRequest = new PushAppRequest();
//        pushAppRequest.setAudience_type(AudienceType.all);
//        pushAppRequest.setPlatform(Platform.android);
//        pushAppRequest.setMessage_type(MessageType.notify);
//        Message message = new Message();
//        message.setTitle(title);
//        message.setContent(content);
//        MessageAndroid messageAndroid = new MessageAndroid();
//        message.setAndroid(messageAndroid);
//        pushAppRequest.setMessage(message);
//        return ceshiapp.pushApp(pushAppRequest);
//    }
//
//
//    /**
//     * IOS 全设备推送
//     * @param title 标题
//     * @param content 内容
//     * @return
//     */
//    public static JSONObject ceshiPushAllIos(String title, String content) {
//        PushAppRequest pushAppRequest = new PushAppRequest();
//        pushAppRequest.setAudience_type(AudienceType.all);
//        pushAppRequest.setEnvironment(Environment.valueOf("dev"));
//        pushAppRequest.setMessage_type(MessageType.notify);
//        Message message = new Message();
//        message.setTitle(title);
//        message.setContent(content);
//        MessageIOS messageIOS = new MessageIOS();
//        Alert alert = new Alert();
//        Aps aps = new Aps();
//        aps.setAlert(alert);
//        messageIOS.setAps(aps);
//        message.setIos(messageIOS);
//        pushAppRequest.setMessage(message);
//        return ceshiiosapp.pushApp(pushAppRequest);
//    }
    public void pushAccountAndroidAndIos(String title, String content, ArrayList<String> accountList) {
        pushAccountIos(title, content, accountList);
        pushAccountAndroid(title, content, accountList, null);
    }

}
